/**
 * SimpleReceiver MicroController program header file.
 *
 * Author: Jimmy F.Klarke, InfiniteSpace Studio
 */

#ifndef ISJFK_SR_MAIN
#define ISJFK_SR_MAIN



#define PORTA_DIR           0xF	    /* port a direction */
#define PORTC_DIR           0xF     /* port c direction */

#define WDT_TIME            2304    /* ms */

#define CH_SIZE             0x10    /* channel parameter size */
#define CH_CMD_SIZE         0x10    /* channel command size */
#define GLOBAL_PARAM_SIZE   0x20    /* global parameter size */
#define SERIAL_NUMBER_SIZE  0x8     /* serial number size */
#define CMD_NUMBER          0xF     /* key to command mapping size */

#define SYS_CMD_TIME        0x2     /* wdt count wait time for sys cmd */

/* state machine */
#define WAIT_CMD            0x0     /* wait for a command */
#define VERIFY_SN           0x1     /* verify serial number */
#define MENU_MAIN           0x2     /* main menu */
#define MENU_CHANNEL        0x3     /* channel parameters */
#define MENU_CH_LEARN       0x4     /* channel learn a new command */
#define MENU_CH_SLS         0x5     /* channel save lasta state */
#define MENU_CH_ISS         0x6     /* channel initial switch state */
#define MENU_CH_AOL         0x7     /* channel always on line */
#define MENU_CH_MODE        0x8     /* channel switch control mode */

/* menu commands */
/* common menu command */
#define CMD_BACK            0xA     /* back to upper menu */
#define CMD_YES             0x1
#define CMD_NO              0x2
/* MENU_MAIN */
#define CMD_CH0             0x1
#define CMD_CH1             0x2
/* MENU_CHANNEL */
#define CMD_LEARN           0x1     /* learn a new command */
#define CMD_CLEAR           0x2     /* clear all learned command */
#define CMD_CH_SLS          0x3     /* enter save lasta state menu */
#define CMD_CH_ISS          0x4     /* enter initial switch state menu */
#define CMD_CH_AOL          0x5     /* enter always on line menu */
#define CMD_CH_MODE         0x6     /* enter switch control mode menu */
/* MENU_CH_ISS */
#define CMD_CH_ON           0x1
#define CMD_CH_OFF          0x2
/* MENU_CH_MODE */
#define CMD_CH_SEP          0x1
#define CMD_CH_FLIP         0x2
#define CMD_CH_CLICK        0x3

#define WDT_TO()            (!TO)   /* watch-dog timeout */



typedef struct {
    /* is auto save last switch state in initSwitch. */
    unsigned saveLastState  : 1;
    /* init switch state, 0 means off and 1 means on. */
    unsigned initSwitch     : 1;
    /* is load attach on always on line. */
    unsigned alwaysOnLine   : 1;
    /* 00 means click one key to turn on and click another key to turn off,
     * 01 means click one key to turn on and click again to turn off,
     * 10 means press one key to turn on and release the key to turn off.
     */
    unsigned switchMode     : 2;
} CH_OPTION;



static volatile bit     SWPB_MODE0      @((unsigned)&PORTA*8)+0;
static volatile bit     SWPB_MODE1      @((unsigned)&PORTA*8)+1;
static volatile bit     DATA_VT         @((unsigned)&PORTA*8)+2;
static volatile bit     SWPB_RESET      @((unsigned)&PORTA*8)+3;
static volatile bit     LED_RED         @((unsigned)&PORTA*8)+4;
static volatile bit     LED_GREEN       @((unsigned)&PORTA*8)+5;

static volatile bit     DATA0           @((unsigned)&PORTC*8)+0;
static volatile bit     DATA1           @((unsigned)&PORTC*8)+1;
static volatile bit     DATA2           @((unsigned)&PORTC*8)+2;
static volatile bit     DATA3           @((unsigned)&PORTC*8)+3;
static volatile bit     CTRL0           @((unsigned)&PORTC*8)+4;
static volatile bit     CTRL1           @((unsigned)&PORTC*8)+5;

static volatile bit     VT              @((unsigned)&PORTA*8)+2;



/* Channel 0 parameters */
eeprom volatile unsigned char CH0[CH_SIZE]          @ 0x00;
/* Channel control bits */
eeprom volatile CH_OPTION     CH0_OPTION            @ 0x00;
/* Size of commands learned, <= 0x10 */
eeprom volatile unsigned char CH0_CMD_SIZE          @ 0x01;
/* Position to write next learned command, wrap when exceed 0x10 */
eeprom volatile unsigned char CH0_CMD_NEXT          @ 0x02;

/* Channel 0 commands. */
eeprom volatile unsigned char CH0_CMDS[CH_CMD_SIZE] @ 0x10;

/* Channel 1 parameters */
eeprom volatile unsigned char CH1[CH_SIZE]          @ 0x20;
/* Channel control bits */
eeprom volatile CH_OPTION     CH1_OPTION            @ 0x20;
/* Size of commands learned, <= 0x10 */
eeprom volatile unsigned char CH1_CMD_SIZE          @ 0x21;
/* Position to write next learned command, wrap when exceed 0x10 */
eeprom volatile unsigned char CH1_CMD_NEXT          @ 0x22;

/* Channel 1 commands. */
eeprom volatile unsigned char CH1_CMDS[CH_CMD_SIZE] @ 0x30;

/* Global parameters. */
eeprom volatile unsigned char GLOBAL_PARAMS[GLOBAL_PARAM_SIZE]  @ 0x40;
/* Serial number */
eeprom volatile unsigned char SERIAL_NUMBER[SERIAL_NUMBER_SIZE] @ 0x40;
/* The command used to enter system menu */
eeprom volatile unsigned char SYSTEM_MENU_CMD                   @ 0x48;
/* The command used to exit system menu */
eeprom volatile unsigned char SYSTEM_EXIT_CMD                   @ 0x49;
/* Key to command mapping table */
eeprom volatile unsigned char KEY_CMD_MAP[CMD_NUMBER]           @ 0x50;

/* Default Channel 0 parameters. */
eeprom volatile unsigned char DEFAULT_CH0[CH_SIZE]  @ 0x60;
/* Default Channel 1 parameters. */
eeprom volatile unsigned char DEFAULT_CH1[CH_SIZE]  @ 0x70;

#endif
